/*
 * SubGrupoRegimenEliminar.java
 *
 * Created on 30 de abril de 2008, 12:40 PM
 */
package org.contabilidad.regimen;

import org.mybeans.Sesion;
import org.mybeans.SubGrupoRegimenBeans;
 
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 *
 * @author  avbravo
 */
public class SubGrupoRegimenEliminarFrame extends javax.swing.JFrame {

    SubGrupoRegimenBeans subGrupoRegimenBeans;
    Sesion sesion;
    boolean focoSubGrupo = false;

    /** Creates new form SubGrupoRegimenEliminar */
    public SubGrupoRegimenEliminarFrame() {
        initComponents();
        /*
         * Fondo
         */

        
          
         

         
         

        /*
         * fin del fondo
         */
        sesion = new Sesion();
        subGrupoRegimenBeans = new SubGrupoRegimenBeans();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldIdSubGrupoRegimen = new javax.swing.JTextField();
        jTextFieldSubGrupoRegimen = new javax.swing.JTextField();
        jButtonConsultarSubGrupoRegimen = new javax.swing.JButton();
        jButtonBuscarSubGrupo = new javax.swing.JButton();
        jButtonRegresar = new javax.swing.JButton();
        jButtonEliminar = new javax.swing.JButton();
        jButtonLimpiar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Eliminar SubGrupo de Regimen");
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel3.setText("Cod. SubGrupo");

        jLabel4.setText("Sub Grupo");

        jTextFieldIdSubGrupoRegimen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldIdSubGrupoRegimenActionPerformed(evt);
            }
        });

        jTextFieldSubGrupoRegimen.setEditable(false);

        jButtonConsultarSubGrupoRegimen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/consultar.png"))); // NOI18N
        jButtonConsultarSubGrupoRegimen.setToolTipText("Consultar");
        jButtonConsultarSubGrupoRegimen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConsultarSubGrupoRegimenActionPerformed(evt);
            }
        });

        jButtonBuscarSubGrupo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/search.png"))); // NOI18N
        jButtonBuscarSubGrupo.setToolTipText("Buscar");
        jButtonBuscarSubGrupo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBuscarSubGrupoActionPerformed(evt);
            }
        });

        jButtonRegresar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButtonRegresar.setToolTipText("Regresar");
        jButtonRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegresarActionPerformed(evt);
            }
        });

        jButtonEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/delete.png"))); // NOI18N
        jButtonEliminar.setToolTipText("Eliminar");
        jButtonEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEliminarActionPerformed(evt);
            }
        });

        jButtonLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/escoba.png"))); // NOI18N
        jButtonLimpiar.setToolTipText("Limpiar");
        jButtonLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLimpiarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(6, 6, 6))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButtonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jButtonLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jTextFieldIdSubGrupoRegimen, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonConsultarSubGrupoRegimen, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonBuscarSubGrupo, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jTextFieldSubGrupoRegimen, javax.swing.GroupLayout.DEFAULT_SIZE, 513, Short.MAX_VALUE))
                        .addGap(24, 24, 24))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldIdSubGrupoRegimen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonConsultarSubGrupoRegimen)
                    .addComponent(jButtonBuscarSubGrupo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldSubGrupoRegimen, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButtonConsultarSubGrupoRegimenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConsultarSubGrupoRegimenActionPerformed
Consultar();
    
}//GEN-LAST:event_jButtonConsultarSubGrupoRegimenActionPerformed

private void jButtonBuscarSubGrupoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBuscarSubGrupoActionPerformed

    SubGrupoRegimenBusquedaFrame subGrupoRegimenBusquedaFrame = new SubGrupoRegimenBusquedaFrame();
    subGrupoRegimenBusquedaFrame.setLocationRelativeTo(null);
    subGrupoRegimenBusquedaFrame.setVisible(true);
    focoSubGrupo = true;
}//GEN-LAST:event_jButtonBuscarSubGrupoActionPerformed

private void jButtonRegresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegresarActionPerformed

    dispose();
}//GEN-LAST:event_jButtonRegresarActionPerformed

private void jButtonEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEliminarActionPerformed

    try {
        String idSubGrupoRegimen = this.jTextFieldIdSubGrupoRegimen.getText();
        if (idSubGrupoRegimen == null || idSubGrupoRegimen.equals("")) {
            JOptionPane.showMessageDialog(this, "Ingrese el codigo ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldIdSubGrupoRegimen.requestFocus();
            return;
        }

        String subgrupo = this.jTextFieldSubGrupoRegimen.getText();
        if (subgrupo == null || subgrupo.equals("")) {
            JOptionPane.showMessageDialog(this, "Ingrese el subgrupo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldIdSubGrupoRegimen.requestFocus();
            return;
        }

        if (sesion.Conectar() == false) {
            JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        String sqlquery = "select * from subgruporegimen where idmunicipio=? and idsubgruporegimen= ? ";
        PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
        psquery.setString(1, sesion.getIdmunicipio());
        psquery.setString(2, idSubGrupoRegimen);
        ResultSet rs = psquery.executeQuery();
        rs.next();
        int x = rs.getRow();

        if (x == 0) {
            JOptionPane.showMessageDialog(this, "No existe un subgrupo con ese codigo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            Limpiar();
            return;
        }
        sqlquery = "select count(*) as total from regimen where idmunicipio=? and idsubgruporegimen= ? ";
        psquery = sesion.con.prepareStatement(sqlquery);
        psquery.setString(1, sesion.getIdmunicipio());
        psquery.setString(2, idSubGrupoRegimen);
        rs = psquery.executeQuery();
        rs.next();
        int total = rs.getInt("total");

        if (total != 0) {
            JOptionPane.showMessageDialog(this, "No se puede eliminar.Hay regimen en ese grupo.", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        int res = JOptionPane.showConfirmDialog(this, "Desea eliminar este subgrupo",
                "Eliminar SubGrupo ", JOptionPane.YES_NO_OPTION);
        if (res == JOptionPane.YES_OPTION) {

            String sql = "delete from subgruporegimen where idmunicipio=? and idsubgruporegimen= ? ";
            PreparedStatement prest = sesion.con.prepareStatement(sql);
            prest.setString(1, sesion.getIdmunicipio());
            prest.setString(2, idSubGrupoRegimen);

            prest.executeUpdate();
            sesion.con.close();
            JOptionPane.showMessageDialog(this, " SubGrupo Eliminado exitosamente", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            Limpiar();
        } else {
            JOptionPane.showMessageDialog(this, "No se elimino el subgrupo.", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        }
    } catch (Exception ex) {
        System.out.println("Error" + ex);
        JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jButtonEliminarActionPerformed

private void jButtonLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLimpiarActionPerformed

    Limpiar();
}//GEN-LAST:event_jButtonLimpiarActionPerformed

private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
    // TODO add your handling code here:
    if (focoSubGrupo) {
        CargarSubGrupos();
    }
    focoSubGrupo = false;
}//GEN-LAST:event_formWindowGainedFocus

private void jTextFieldIdSubGrupoRegimenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldIdSubGrupoRegimenActionPerformed
    // TODO add your handling code here:
    Consultar();
}//GEN-LAST:event_jTextFieldIdSubGrupoRegimenActionPerformed

private void Consultar(){
    try {
        String idSubGrupoRegimen = this.jTextFieldIdSubGrupoRegimen.getText();
        if (idSubGrupoRegimen == null || idSubGrupoRegimen.equals("")) {
            JOptionPane.showMessageDialog(this, "Ingrese el codigo del subgrupo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        if (sesion.Conectar() == false) {
            JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        String sqlquery = "select * from subgruporegimen where idmunicipio = ? and idsubgruporegimen = ?";
        PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
        psquery.setString(1, sesion.getIdmunicipio());
        psquery.setString(2, idSubGrupoRegimen);
        ResultSet rs = psquery.executeQuery();
        rs.next();
        int x = rs.getRow();
        if (x == 0) {
            JOptionPane.showMessageDialog(this, "No existen registros de subgrupo con ese codigo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        String activo = rs.getString("activo");
        if (activo.equals("no")) {
            JOptionPane.showMessageDialog(this, "Este registro esta inactivo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        String subgrupo = rs.getString("subgrupo");

        if (subgrupo == null || subgrupo.equals("")) {
            JOptionPane.showMessageDialog(this, "El grupo esta en blanco", "Mensaje", JOptionPane.WARNING_MESSAGE);
            return;
        }
        subGrupoRegimenBeans.setIdSubGrupoRegimen(idSubGrupoRegimen);
        subGrupoRegimenBeans.setSubGrupo(subgrupo);
        this.jTextFieldIdSubGrupoRegimen.setText(idSubGrupoRegimen);
        this.jTextFieldSubGrupoRegimen.setText(subgrupo);
        repaint();

    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new SubGrupoRegimenEliminarFrame().setVisible(true);
            }
        });
    }

    private void Limpiar() {
        jTextFieldIdSubGrupoRegimen.setText("");
        jTextFieldSubGrupoRegimen.setText("");
    }

    public void CargarSubGrupos() {
        try {

            this.jTextFieldIdSubGrupoRegimen.setText(subGrupoRegimenBeans.getIdSubGrupoRegimen());
            this.jTextFieldSubGrupoRegimen.setText(subGrupoRegimenBeans.getSubGrupo());

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonBuscarSubGrupo;
    private javax.swing.JButton jButtonConsultarSubGrupoRegimen;
    private javax.swing.JButton jButtonEliminar;
    private javax.swing.JButton jButtonLimpiar;
    private javax.swing.JButton jButtonRegresar;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextFieldIdSubGrupoRegimen;
    private javax.swing.JTextField jTextFieldSubGrupoRegimen;
    // End of variables declaration//GEN-END:variables
}
